# f = open('测试.txt', 'r') # 打开文件
# all_content = f.read() # 读取文件中所有内容
# from collections import Iterable
# print(isinstance(f, Iterable))
# print(all_content)
# # first_paragraph = f.readline() # 读取文件中第一行内容
# for line in f.readlines():
#     print(line) # 打印每一行
# f.close() # 关闭文件句柄

# fr = open('测试.txt', 'r') # 打开文件
# fw = open('测试.txt', 'w') # 打开文件
#
# fw.write('新的内容')
# print('读取：', fr.read())
#
# fr.close()
# fw.close()

# 使用with关键字
# with open('测试.txt', 'r') as f:
#     all_content = f.read()
#     print(all_content)

# class myopen():
#     '''模拟使用with关键字的open方法'''
#     def __init__(self, filepath, mode):
#         self.filepath = filepath
#         self.mode = mode
#
#     def __enter__(self):
#         print('打开文件')
#         self.f = open(self.filepath, self.mode)
#         return self.f
#
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         print('关闭文件')
#         self.f.close()
#
# with myopen('测试.txt', 'r') as f:
#     res = f.read()
#     print(res)

# with open('测试.txt', 'r', encoding='utf-8') as f:
#     print(f.read(11))

# with open('测试.txt', 'w', encoding='utf-8') as f:
#     f.write('Python自动化任务，cool~')

# with open('测试.txt', 'a', encoding='utf-8') as f:
#     f.write('Python自动化任务，cool~')

# with open('测试.txt', 'a', encoding='utf-8') as f:
#     f.write('新添加的内容')

# with open('测试2.txt', 'wb') as f:
#     f.write('新添加的内容'.encode('utf-8'))
#
# with open('测试2.txt', 'rb') as f:
#     print(f.read())
#     # b'\xe6\x96\xb0\xe6\xb7\xbb\xe5\x8a\xa0\xe7\x9a\x84\xe5\x86\x85\xe5\xae\xb9'
import time
with open('测试3.txt', 'x') as f:
    # The 'x' mode implies 'w' and
    #     raises an `FileExistsError` if the file already exists.
    time.sleep(10)
    f.write('新添加的内容')